﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Diagnostics;

namespace myBoard.Framework.Controls
{
	/// <summary>
	/// Interaction logic for PreLoader.xaml
	/// </summary>
	public partial class PreLoader
	{
        public int Progress
        {
            get { return (int)GetValue(ProgressProperty); }
            set { SetValue(ProgressProperty, value); }
        }

        public bool AutoRemoveFromContainer
        {
            get { return (bool)GetValue(AutoRemoveFromContainerProperty); }
            set { SetValue(AutoRemoveFromContainerProperty, value); }
        }

        public SolidColorBrush RingBackground
        {
            get { return (SolidColorBrush)GetValue(RingBackgroundProperty); }
            set { SetValue(RingBackgroundProperty, value); }
        }

        public SolidColorBrush RingBorder
        {
            get { return (SolidColorBrush)GetValue(RingBorderProperty); }
            set { SetValue(RingBorderProperty, value); }
        }

        public SolidColorBrush TailColor
        {
            get { return (SolidColorBrush)GetValue(TailColorProperty); }
            set { SetValue(TailColorProperty, value); }
        }

        public static readonly DependencyProperty ProgressProperty =
            DependencyProperty.Register("Progress", typeof(int), typeof(PreLoader), new UIPropertyMetadata(OnProgressChanged));
        
        public static readonly DependencyProperty AutoRemoveFromContainerProperty =
            DependencyProperty.Register("AutoRemoveFromContainer", typeof(bool), typeof(PreLoader), new UIPropertyMetadata(false));

        public static readonly DependencyProperty RingBackgroundProperty =
            DependencyProperty.Register("RingBackground", typeof(SolidColorBrush), typeof(PreLoader), new UIPropertyMetadata(RingBackgroundChanged));

        public static readonly DependencyProperty RingBorderProperty =
            DependencyProperty.Register("RingBorder", typeof(SolidColorBrush), typeof(PreLoader), new UIPropertyMetadata(RingBorderChanged));

        public static readonly DependencyProperty TailColorProperty =
            DependencyProperty.Register("TailColor", typeof(SolidColorBrush), typeof(PreLoader), new UIPropertyMetadata(TailColorChanged));

        static void OnProgressChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            PreLoader preLoader = sender as PreLoader;
            preLoader._progressLabel.Content = string.Format("{0} %", e.NewValue);

            if (preLoader.Progress == 100)
                preLoader.Stop();
        }

        private static void RingBackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PreLoader preLoader = d as PreLoader;
            preLoader._ring.Fill = e.NewValue as SolidColorBrush;
        }        

        private static void RingBorderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PreLoader preLoader = d as PreLoader;
            preLoader._ring.Stroke = e.NewValue as SolidColorBrush;
        }

        private static void TailColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PreLoader preLoader = d as PreLoader;
            preLoader._tail.Fill = e.NewValue as SolidColorBrush;
        } 

        Storyboard _loopSB;
        Storyboard _introSB;
        Storyboard _outroSB;
        bool _alreadyLooping;

		public PreLoader()
		{
			this.InitializeComponent();
            _loopSB = this.Resources["Loop"] as Storyboard;
            _introSB = this.Resources["Intro"] as Storyboard;
            _outroSB = this.Resources["Outro"] as Storyboard;
		}

        public void Start()
        {
            if (!_alreadyLooping)
            {
                this.BeginStoryboard(_loopSB);
                _alreadyLooping = true;
            }

            this.BeginStoryboard(_introSB);
        }

        public void Stop()
        {
            _outroSB.Completed += new EventHandler(delegate(object sender, EventArgs e)
                {
                    if (AutoRemoveFromContainer && this.Parent is Panel)
                        ((Panel)this.Parent).Children.Remove(this);                    

                });
            this.BeginStoryboard(_outroSB);
        }
	}
}